package cn.com.dycc.sys_manage.usersysmanagement.service;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.baidu.yun.push.exception.PushClientException;
import com.baidu.yun.push.exception.PushServerException;

import cn.com.dycc.common.attach.service.AttachService;
import cn.com.dycc.common.log.Constants;
import cn.com.dycc.common.log.SystemServiceLog;
import cn.com.dycc.common.push_key.dao.UserChannelDao;
import cn.com.dycc.party_org.member.dao.MemberDao;
import cn.com.dycc.party_org.member.entity.Member;
import cn.com.dycc.party_org.member.param.MemberParam;
import cn.com.dycc.push.BaiduPush;
import cn.com.dycc.push.PushInfo;
import cn.com.dycc.sys_manage.deptsysmanagement.dao.DeptDao;
import cn.com.dycc.sys_manage.dictionarysysmanagement.dao.DictionaryDao;
import cn.com.dycc.sys_manage.logsysmanagement.dao.OperateLogDao;
import cn.com.dycc.sys_manage.rolesysmanagement.dao.RoleUserDao;
import cn.com.dycc.sys_manage.rolesysmanagement.entity.RoleUser;
import cn.com.dycc.sys_manage.rolesysmanagement.service.RoleNotFoundException;
import cn.com.dycc.sys_manage.usersysmanagement.dao.UserChildAccountDao;
import cn.com.dycc.sys_manage.usersysmanagement.dao.UserDao;
import cn.com.dycc.sys_manage.usersysmanagement.entity.Conver;
import cn.com.dycc.sys_manage.usersysmanagement.entity.NewUser;
import cn.com.dycc.sys_manage.usersysmanagement.entity.User;
import cn.com.dycc.sys_manage.usersysmanagement.entity.UserChildAccount;
import cn.com.dycc.sys_manage.usersysmanagement.entity.UserIndexResult;
import cn.com.dycc.sys_manage.usersysmanagement.entity.UserInfo;
import cn.com.dycc.sys_manage.usersysmanagement.entity.UserInfoAccount;
import cn.com.dycc.sys_manage.usersysmanagement.entity.UserPart;
import cn.com.dycc.sys_manage.usersysmanagement.entity.UserResult;
import cn.com.dycc.sys_manage.usersysmanagement.param.AddUserParam;
import cn.com.dycc.sys_manage.usersysmanagement.param.UserParam;
import cn.com.dycc.util.DateUtil;
import cn.com.dycc.util.FirstChar;
import cn.com.dycc.util.IPUtil;
import cn.com.dycc.util.IntegerUtil;
import cn.com.dycc.util.NoteUtil;
import cn.com.dycc.util.Paging;
import cn.com.dycc.util.PathUtil;
import cn.com.dycc.util.PropertiesUtil;
import net.sf.json.JSONObject;
/**
 * 用户信息业务层
 * Description: 
 * Author: linfangan
 * Version: 1.0
 * Create Date Time: 2017年8月14日 下午2:57:35.
 * Update Date Time: 
 * @see
 */
@Service("userService")
public class UserServiceImpl implements UserService{
	
	@Resource(name="userDao")
	private UserDao userDao;
	
	
	@Resource(name="roleUserDao")
	private RoleUserDao roleUserDao;
	
	@Resource(name="userChildAccountDao")
	private UserChildAccountDao userChildAccountDao;
	
	@Resource(name="paging")
	private Paging paging;
	
	@Resource(name="dictionaryDao")
	private DictionaryDao dictionaryDao;
	
	@Resource
	private OperateLogDao operateLogDao;
	
	@Resource(name="attachService")
	private AttachService attachService;
	
	@Resource
	private DeptDao deptDao;
	
	@Resource
	private UserChannelDao userChannelDao;
	
	@Resource
	private MemberDao memberDao;
	
	
	//新增用户信息
	@Transactional
	@SystemServiceLog(description=Constants.T_USER_ADD)
	public String insertUser(AddUserParam user) throws UserNotFoundException,
		PasswordNotFoundException,CeritficateNotFoundException,UserChildAccountNoutFoundException,RoleNotFoundException{
		
//		System.out.println("sss="+" "+user);
		//用户基本信息
		//用户账号
		String userAccount = user.getAccount();
		if(userAccount==null||userAccount.trim().isEmpty()){
			throw new UserNotFoundException("用户账号不允许为空！");
		}
		//判断账户是否已经存在
		User find_id = userDao.findUserByAccount(userAccount);
		if(find_id!=null){
			throw new UserNotFoundException("账号已存在！");
		}
		//用户密码
		String userPassword = user.getPassword();
		if(userPassword==null||userPassword.trim().isEmpty()){
			throw new PasswordNotFoundException("登录密码不能为空！");
		}
//		System.out.println(userPassword);
//		String passwordReg = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$";
		String passwordReg = "^((?=.*[a-z])(?=.*\\d)|(?=[a-z])(?=.*[#@!~%^&*])|(?=.*\\d)(?=.*[#@!~%^&*]))[a-z\\d#@!~%^&*]{6,20}$";
		if(!Pattern.matches(passwordReg,userPassword)){
			throw new PasswordNotFoundException("登录密码需要填写6-20位字母数字组合！");
		}
		//密码加密
		String Md5UserPassword = NoteUtil.md5(userPassword);
		//用户姓名
		String userName = user.getUsername();
		if(userName==null||userName.trim().isEmpty()){
			throw new UserNotFoundException("用户姓名不能为空！");
		}
		//用户昵称
		String userNickName = user.getNickname();
		if(userNickName.length()==0){
			userNickName = userName;
		}
		//用户身份证号
		String userCertificate = user.getCertificate();
		if(!(userCertificate.length()==0)){
			String userCertificateReg = "(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)";
			if(!Pattern.matches(userCertificateReg,userCertificate)){
				throw new CeritficateNotFoundException("请填写正确的身份证号码！");
			}
		}
		//用户其他信息
		//手势密码
		//String userHandPwd = user.getHand_pwd();
		String userCardNo = user.getCard_no();
		String userAddr = user.getAddr();
		//头像
	//	String userFace = user.getFace();
		String userPoliticalStatus = user.getPolitical_status();
		//获取操作人,登录人
		String user_id = user.getCreate_user();
		//获取组织id
		String org_id = user.getOrg_id();
		String state = user.getState();
//		User zjUser = new User();
		
	
		//移动号码
		String mobile_tel = user.getMobile_tel().trim();
		if(mobile_tel==null||mobile_tel.trim().isEmpty()){
			throw new UserNotFoundException("请填写手机号码！");
		}
//		System.out.println("mobile_tel="+mobile_tel);
		if(!(IntegerUtil.isNumeric(mobile_tel))){
			throw new UserNotFoundException("手机号码只能为整型！");
		}
		if(!(IntegerUtil.isMobile(mobile_tel))){
			throw new UserNotFoundException("请填写正确的手机号码！");
		}
		
		//固定电话
		String home_tel = user.getHome_tel().trim();
//		System.out.println("home_tel="+home_tel);
		if(home_tel!=null&&!("".equals(home_tel))){
//			if(!(IntegerUtil.isNumeric(home_tel))){
//				throw new UserNotFoundException("电话号码只能为整型！");
//			}
			if(!(IntegerUtil.isPhone(home_tel))){
				throw new UserNotFoundException("请填写正确的电话号码！");
			}
		}
		
		//获取用户名称的全拼音	
		String name_py = FirstChar.getFullSpell(userName);
		//获取用户名称每个中文字的首个拼音
		String name_py_short = FirstChar.getFisrtChar(userName);
		//获取用户名称的首个拼音
		String name_py_first = name_py_short.substring(0,1);
		
		NewUser zjUser = new NewUser();
		Integer userOrderNo = user.getOrder_no();
		if(userOrderNo!=null&&!(IntegerUtil.isNumeric(userOrderNo.toString()))){
			throw new UserNotFoundException("排序只能为整型！");
		}
		zjUser.setName_py(name_py);
		zjUser.setName_py_first(name_py_first);
		zjUser.setName_py_short(name_py_short);
		zjUser.setState(state);
		zjUser.setAccount(userAccount);
		zjUser.setPassword(Md5UserPassword);
//		zjUser.setHand_pwd(userHandPwd);
		zjUser.setUsername(userName);
		zjUser.setNickname(userNickName);
		zjUser.setCertificate(userCertificate);
		zjUser.setCard_no(userCardNo);
		zjUser.setAddr(userAddr);
//		zjUser.setFace(userFace);
		zjUser.setOrder_no(userOrderNo);
		zjUser.setPolitical_status(userPoliticalStatus);
		zjUser.setCreate_user(user_id);
		zjUser.setOrg_id(org_id);
		zjUser.setHome_tel(home_tel);
		zjUser.setMobile_tel(mobile_tel);
//		System.out.println("zjUser="+zjUser);
//		zjUser.setUpdate_user_id(user_id);
		//时间
//		String format = "yyyy-MM-dd HH:mm:ss";
//		String update_date = DateUtil.getNowDate(format);
//		zjUser.setUpdate_date(update_date);
		//增加User基本信息
		int addU = userDao.addUser(zjUser);
		if(addU<=0){
//			return "增加失败！";
			throw new UserNotFoundException("添加用户失败！");
		}
		//返回用户id
		String id = zjUser.getId();
		
//		System.out.println("测试用户123");
		
		
//		//增加用户子账户
		UserChildAccount userChildAccount = user.getUserChildAccount();
		UserChildAccount addUserchild = new UserChildAccount();
//		设置账户职务
		String userChildClassNo = userChildAccount.getClass_no();
//		设置岗位
		String userChildPostNo = userChildAccount.getPost_no();
//		//设置所属单位
//		String userChildTopDeptNo = userChildAccount.getTop_dept_no();
//		if(userChildTopDeptNo==null||userChildTopDeptNo==""){
//			throw new UserChildAccountNoutFoundException("请选择用户单位");
//		}
//		//设置所属部门
		String userChildDeptNo = userChildAccount.getDept_no();
		if(userChildDeptNo==null||userChildDeptNo==""){
			throw new UserChildAccountNoutFoundException("请选择用户部门");
		}
		
		//根据部门id查找查询pid
		String userChildTopDeptNo = deptDao.findPidBydeptNo(userChildDeptNo,org_id);
		
		
//		System.out.println("3123131313");
//		System.out.println("bushi");
		//用户账户
		addUserchild.setAccount(userAccount);
		//用户id
		addUserchild.setUser_id(id);
		addUserchild.setClass_no(userChildClassNo);
//		System.out.println("nizai");
		addUserchild.setPost_no(userChildPostNo);
		addUserchild.setDept_no(userChildDeptNo);
		//默认账号
		addUserchild.setPrincipal("1");
		addUserchild.setTop_dept_no(userChildTopDeptNo);
		addUserchild.setOrg_id(org_id);
		addUserchild.setOpt_user_id(user_id);
//		System.out.println("user_id");
//		addUserchild.setUpdate_user_id(user_id);
//		System.out.println("user_id2");
//		System.out.println(update_date);
//		addUserchild.setUpdate_date(update_date);
//		System.out.println("31");
		addU = userChildAccountDao.addUserChildAccount(addUserchild);
		if(addU<=0){
			throw new UserNotFoundException("添加用户失败！");
		}
		
//		System.out.println("测试456");
		
		//返回用户子表id
		//String account_id = addUserchild.getId();
		List<RoleUser> listRoleUser = user.getListRoleUser();
		
		if(!(listRoleUser.isEmpty())){
			//用户授权
			RoleUser addRoleUser = new RoleUser();
			for(RoleUser aloneRoleUser : listRoleUser){
				//获取角色id
				String roleUserRoleNo = aloneRoleUser.getRole_no();
				addRoleUser.setRole_no(roleUserRoleNo);
				addRoleUser.setUser_id(id);
				addU = roleUserDao.addRoleUser(addRoleUser);
				if(addU<=0){
//					return "增加失败！";
					throw new UserNotFoundException("添加用户失败！");
				}
			}
		}
//		System.out.println("测试789");
		return "增加成功！";
	}
	
	
	
	//通过id查找主账号的所有信息
	public UserResult findAllUserMessageByUserId(HttpServletRequest request,String id) throws UserNotFoundException,RoleNotFoundException{
		if(id==null){
			throw new UserNotFoundException("id不能为空");
		}
		
		String org_id = request.getSession().getAttribute("org_id").toString();
		UserResult userMessage = userDao.findAllUserInfoByUserId(id,org_id);
//		System.out.println(userMessage);
		if(userMessage==null){
			throw new UserNotFoundException("该账户未存在！");
		}
		return userMessage;
	}
//	
//	//分页查询T_user表的全部信息
//	public List<Map<String, Object>> findAllUserInfo(int size, int page,List<Integer> bindRoleNo) throws UserNotFoundException,RoleNotFoundException {
//		//判断是否为系统管理员或者是用户管理员
//		boolean guess = true;
//		for(int i=0;i<bindRoleNo.size();i++){
//			Integer bind = bindRoleNo.get(i);
//			if(bind==9||bind==13||bind==7||bind==4){
//				guess = false;
//				break;
//			}
//		}
//		if(guess){
//			throw new RoleNotFoundException("用户管理需要权限为系统管理员或用户管理员或普通管理员！");
//		}
//		int start=page*size;  //start 第几条开始
//		List<Map<String,Object>> list  = userDao.findAllUserInfo(start, size);
//		return list;
//	}
//	
	
	//根据id删除相关信息
	@Transactional
	@SystemServiceLog(description=Constants.T_USER_DELETE)
	public String deleteUserInfoByUserId(HttpServletRequest request,String userId) throws UserNotFoundException,RoleNotFoundException{
		if(userId==null){
			throw new UserNotFoundException("请选择需要删除的用户");
		}
		List<String> list_role = roleUserDao.findRoleNoByUserId(userId);
		if(list_role.size()>0){
			throw new UserNotFoundException("该用户已被分配了权限！");
		}
		String org_id = request.getSession().getAttribute("org_id").toString();
		int delall = userDao.deleteUserInfoByUserId(userId,org_id);
		if(delall<=0){
			throw new UserNotFoundException("删除失败！");
		}
		return "删除成功！";
	}
	
//	
	//更新用户信息
	@Transactional
	@SystemServiceLog(description=Constants.T_USER_UPDATE)
	public String updateUserInfoByUserId(AddUserParam user)throws UserNotFoundException, 
		PasswordNotFoundException, CeritficateNotFoundException,UserChildAccountNoutFoundException,RoleNotFoundException {
		//获取用户id
		String id = user.getId();
//		System.out.println(id);
		if(id==null){
			throw new UserNotFoundException("请选择需要更新的用户");
		}
		String userAccount = user.getAccount();
		if(userAccount.length()==0||userAccount.trim().isEmpty()){
			throw new UserNotFoundException("用户账号不允许为空！");
		}
		//判断账户是否已经存在
		String find_account = userDao.findAccountByUserId(id,user.getOrg_id());
//		System.out.println("find_account="+find_account);
		if(!(find_account.equals(userAccount))){
			User find_id = userDao.findUserByAccount(userAccount);
			if(find_id!=null){
				throw new UserNotFoundException("账号已存在！");
			}
		}
//		//用户密码
//		String userPassword = user.getPassword();
//		if(userPassword.length()==0||userPassword.trim().isEmpty()){
//			throw new PasswordNotFoundException("登录密码不能为空！");
//		}
//		System.out.println(userPassword);
//		String passwordReg = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$";
//		if(!Pattern.matches(passwordReg,userPassword)){
//			throw new PasswordNotFoundException("登录密码需要填写6-20位字母数字组合！");
//		}
//		//密码加密
//		String Md5UserPassword = NoteUtil.md5(userPassword);
		//用户姓名
		String userName = user.getUsername();
		if(userName.length()==0||userName.trim().isEmpty()){
			throw new UserNotFoundException("用户姓名不能为空！");
		}
		//用户昵称
		String userNickName = user.getNickname();
		if(userNickName.length()==0){
			userNickName = userName;
		}
		//用户身份证号
		String userCertificate = user.getCertificate();
		if(!(userCertificate.length()==0)){
			String userCertificateReg = "(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)";
			if(!Pattern.matches(userCertificateReg,userCertificate)){
				throw new CeritficateNotFoundException("请填写正确的身份证号码！");
			}
		}
		//用户其他信息
		//获取用户名称的全拼音	
		String name_py = FirstChar.getFullSpell(userName);
		//获取用户名称每个中文字的首个拼音
		String name_py_short = FirstChar.getFisrtChar(userName);
		//获取用户名称的首个拼音
		String name_py_first = name_py_short.substring(0,1);
		//手势密码
		//String userHandPwd = user.getHand_pwd();
		String userCardNo = user.getCard_no();
		String userAddr = user.getAddr();
		//头像
	//	String userFace = user.getFace();
		String userPoliticalStatus = user.getPolitical_status();
		//获取操作人,登录人
		String update_user_id = user.getUpdate_user_id();
		//获取组织id
		String org_id = user.getOrg_id();
		
		//移动号码
		String mobile_tel = user.getMobile_tel().trim();
//		System.out.println("mobile_tel="+mobile_tel);
		if(mobile_tel==null||mobile_tel.trim().isEmpty()){
			throw new UserNotFoundException("请填写手机号码！");
		}
		if(!(IntegerUtil.isNumeric(mobile_tel))){
			throw new UserNotFoundException("手机号码只能为整型！");
		}
		if(!(IntegerUtil.isMobile(mobile_tel))){
			throw new UserNotFoundException("请填写正确的手机号码！");
		}
		
		//固定电话
		String home_tel = user.getHome_tel().trim();
//		System.out.println("home_tel="+home_tel);
		if(home_tel!=null&&!("".equals(home_tel))){
//			if(home_tel!=null&&!(IntegerUtil.isNumeric(home_tel))){
//				throw new UserNotFoundException("手机号码只能为整型！");
//			}
			if(home_tel!=null&&!(IntegerUtil.isPhone(home_tel))){
				throw new UserNotFoundException("请填写正确的电话号码！");
			}
		}
		
		
//		User zjUser = new User();
		NewUser zjUser = new NewUser();
		Integer userOrderNo = user.getOrder_no();
		if(userOrderNo==null){
			zjUser.setOrder_no(null);
		}
		zjUser.setHome_tel(home_tel);
		zjUser.setMobile_tel(mobile_tel);
		zjUser.setName_py(name_py);
		zjUser.setName_py_first(name_py_first);
		zjUser.setName_py_short(name_py_short);
		String state = user.getState();
		zjUser.setId(id);
		zjUser.setState(state);
		zjUser.setAccount(userAccount);
//		zjUser.setPassword(Md5UserPassword);
//		zjUser.setHand_pwd(userHandPwd);
		zjUser.setUsername(userName);
		zjUser.setNickname(userNickName);
		zjUser.setCertificate(userCertificate);
		zjUser.setCard_no(userCardNo);
		zjUser.setAddr(userAddr);
//		zjUser.setFace(userFace);
		zjUser.setOrder_no(userOrderNo);
		zjUser.setPolitical_status(userPoliticalStatus);
		zjUser.setUpdate_user_id(update_user_id);
		zjUser.setOrg_id(org_id);
//		System.out.println("zjUser="+zjUser);
		//时间
		String format = "yyyy-MM-dd HH:mm:ss";
		String update_date = DateUtil.getNowDate(format);
		zjUser.setUpdate_date(update_date);
		//更新User基本信息
		int addU = userDao.updateUser(zjUser);
	//	System.out.println(zjUser);
		if(addU<=0){
//			return "更新失败！";
			throw new UserNotFoundException("更新用户失败！");
		}
		
		UserChildAccount userChildAccount = user.getUserChildAccount();
		//更新用户子账户
		UserChildAccount upUserchild = new UserChildAccount();
		//设置账户职务
		String userChildClassNo = userChildAccount.getClass_no();
		//设置岗位
		String userChildPostNo = userChildAccount.getPost_no();
		//设置所属单位
//		String userChildTopDeptNo = userChildAccount.getTop_dept_no();
//		if(userChildTopDeptNo==null||userChildTopDeptNo==""){
//			throw new UserChildAccountNoutFoundException("请选择用户单位");
//		}
		//设置所属部门
		String userChildDeptNo = userChildAccount.getDept_no();
		if(userChildDeptNo==null||userChildDeptNo==""){
			throw new UserChildAccountNoutFoundException("请选择用户部门");
		}
		
		//根据部门id查找查询pid
		String userChildTopDeptNo = deptDao.findPidBydeptNo(userChildDeptNo,org_id);
		
		String userChildprin = userChildAccount.getPrincipal();
//		String child_state = userChildAccount.getState();
		//获取子表id
		String account_id = userChildAccountDao.findAccountIdByUserId(id);
		//用户id
		upUserchild.setId(account_id);
		upUserchild.setUser_id(id);
		upUserchild.setAccount(userAccount);
		upUserchild.setState(state);
		upUserchild.setClass_no(userChildClassNo);
		upUserchild.setPost_no(userChildPostNo);
		upUserchild.setPrincipal(userChildprin);
		upUserchild.setDept_no(userChildDeptNo);
		upUserchild.setTop_dept_no(userChildTopDeptNo);
		upUserchild.setUpdate_date(update_date);
		upUserchild.setUpdate_user_id(update_user_id);
		upUserchild.setOrg_id(org_id);
		addU = userChildAccountDao.updateUserAccount(upUserchild);
//		System.out.println(upUserchild);
		if(addU<=0){
//			return "更新失败！";
			throw new UserNotFoundException("更新用户失败！");
		}
	
		//用户授权
		RoleUser addRoleUser = new RoleUser();
		List<RoleUser> listRoleUser = user.getListRoleUser();
		if(!listRoleUser.isEmpty()){
			//先删除原先授权
//			int delroleuser = roleUserDao.deleteRoleUserByUserId(account_id);
			roleUserDao.deleteRoleUserByUserId(id);
			for(RoleUser aloneRoleUser : listRoleUser){
				//获取角色id
				String roleUserRoleNo = aloneRoleUser.getRole_no();
				addRoleUser.setRole_no(roleUserRoleNo);
				//不是默认账号，绑定子表id
				addRoleUser.setUser_id(id);
				addU = roleUserDao.addRoleUser(addRoleUser);
//				System.out.println(addRu);
				if(addU<=0){
					throw new UserChildAccountNoutFoundException("添加失败！");
				}
			}
		}
		return "更新成功！";
	}
	
	//批量删除用户信息（多表）
	@Transactional
	public String delBatchUser(HttpServletRequest request,String... ids)
			throws UserNotFoundException, RoleNotFoundException {
		
		if(ids.length==0){
			throw new UserNotFoundException("请选择需要删除的人员信息!");
		}
		for(int i = 0; i<ids.length;i++){
			List<String> list_role = roleUserDao.findRoleNoByUserId(ids[i]);
			if(list_role.size()>0){
				throw new UserNotFoundException("选中的用户已经有用户分配角色!");
			}
		}
		
		String org_id = request.getSession().getAttribute("org_id").toString();
		
		List<String> listString = Arrays.asList(ids);
		
		int del = userDao.delBatchUser(listString,org_id);
		if(del<=0){
			throw new UserNotFoundException("删除失败!");
		}
		return "删除成功！";
	}
	
	@Transactional
	@SystemServiceLog(description=Constants.LOGIN)
	public boolean login(String account, String password,HttpServletRequest req) throws UserNotFoundException, PasswordNotFoundException {
		if(account==null||account.trim().isEmpty()){
			throw new UserNotFoundException("账号为空！");
		}
		if(password==null||password.trim().isEmpty()){
			throw new PasswordNotFoundException("密码为空！");
		}
//		int num = operateLogDao.countLogin(account);
////		System.out.println("num="+num);
//		if(num>=5){
//			throw new UserNotFoundException("该账号已限制登录，请与管理员联系！");
//		}
		password = NoteUtil.md5(password);
		
		List<Map<String,Object>> list = userDao.findUserAllInfoByAccount(account);
		//System.out.println("list"+list);
//		for(Map<String,Object> map:list){
//			System.out.println(map);
//		}
		if(list.isEmpty()){
			throw new UserNotFoundException("用户名不存在");
		}
		for(Map<String,Object> map:list){
			if(map.containsKey("state")){
				if(!"Y".equals(map.get("state"))){
					throw new UserNotFoundException("该用户不允许登录");
				}				
			}
			if(map.containsKey("password")){
				if(!password.equals(map.get("password"))){
					throw new PasswordNotFoundException("密码错误");
				}					
			};
		}	
		
		HttpSession session = req.getSession();
		//获取IP地址
		String ip = IPUtil.getLoginIp(req);
//		String ip = req.getHeader("x-forwarded-for");
//	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
//	        ip = req.getHeader("Proxy-Client-IP"); 
//	    }  
//	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
//	        ip = req.getHeader("WL-Proxy-Client-IP");
//	    }  
//	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
//	        ip = req.getRemoteAddr();	       
//	    }
	    
	    String id = null;
	    for(Map<String,Object> map:list){
	    	id = (String) map.get("id");
			session.setAttribute("id", id);//用户ID
			session.setAttribute("username", map.get("username"));//用户姓名
			session.setAttribute("nickname", map.get("nickname"));//用户昵称
			session.setAttribute("dept_no", map.get("dept_no"));//部门号
			session.setAttribute("top_dept_no", map.get("top_dept_no"));//组织号
			session.setAttribute("dept_flag", map.get("dept_flag"));//业主or监理or施工
			session.setAttribute("icon_url", map.get("face"));	//头像
			session.setAttribute("org_id", map.get("org_id"));//组织标识
			//System.out.println("dept_flag="+map.get("dept_flag"));
			//System.out.println("icon_url="+map.get("face"));
				
		}
	    List<String> role_nos1 = roleUserDao.findRoleNoByUserId(id);
	    if(role_nos1.isEmpty()){
	    	throw new UserNotFoundException("用户未分配权限，请联系管理员");
	    }
	    //List<String> role_nos2 = roleUserDao.findRoleNoByUserIdAndChild(id);
	    //role_nos1.addAll(role_nos2);
//	    System.out.println("role_nos1"+role_nos1);
	    session.setAttribute("role_no", role_nos1);//角色号
		session.setAttribute("account",account);//账号
		session.setAttribute("userip", ip);//用户登录IP
//		session.setAttribute("org_id", "1");//组织标识
//		System.out.println("1");
		MemberParam param = new MemberParam();
		param.setUser_id(id);
		param.setOrg_id((String)session.getAttribute("org_id"));
		Member member = memberDao.findMember(param);
		
		String dept_id=member==null?"":member.getDept_id();
		session.setAttribute("dept_id", dept_id);//当组织ID
		return true;
	}


	//多条件精确查询用户
	public List<UserResult> findUserByArray(HttpServletRequest request,UserParam userParam,String org_id,String state)
			throws UserNotFoundException{
		
		String dept_no = request.getSession().getAttribute("dept_no").toString();
		if(dept_no==null){
			throw new UserNotFoundException("请登录！");
		}
		
		//递归查询父往下查所有部门id
		List<String> list_Dept_no = deptDao.findDeptBylowPid(dept_no);
		
		Integer page = userParam.getPage();
		Integer limit = userParam.getLimit();
		Integer start = null;
		
		if(limit==null&&page!=null){
			limit = paging.getPageSize();
			start=(page-1)*limit;
		}
		if(page==null){
			start = null;
			limit = null;
		}
		if(limit!=null&&page!=null){
			start = limit*(page-1);
		}
		userParam.setOrg_id(org_id);
		userParam.setStart(start);
		userParam.setState(state);
		userParam.setListDept_no(list_Dept_no);
		
		List<UserResult> listUserResult = new ArrayList<UserResult>();
		
		try {
			
			listUserResult = userDao.findUserByArray(userParam);
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new UserNotFoundException("查询失败!请稍后重试");
		}	
		return listUserResult;
	}


	//查询角色101和102内的人员信息
	public List<UserInfo> findUserInfoByRoleUser(String dept_no,String user_id) throws UserNotFoundException {
		if(dept_no==null){
			throw new UserNotFoundException("请选择部门!");
		}
		if(user_id==null){
			throw new UserNotFoundException("请选择人员!");
		}
		
		List<UserInfo> listInfo = new ArrayList<UserInfo>();
		try {
			List<UserInfo> list = userDao.findUserInfoByRoleUser(dept_no);
			String top_dept_no = userChildAccountDao.findTopDepNotByDeptNo(dept_no, user_id);
			List<UserInfo> listTop = userDao.findUserInfoByRoleUserByTop(top_dept_no);
			listInfo.addAll(list);
			listInfo.addAll(listTop);
		} catch (Exception e) {
			e.printStackTrace();
			throw new UserNotFoundException("查询失败!请稍后重试");
		}
		//去重
		  List<UserInfo> listTemp = new ArrayList<UserInfo>();  
	        for(int i=0;i<listInfo.size();i++){  
	            if(!listTemp.contains(listInfo.get(i))){  
	                listTemp.add(listInfo.get(i));  
	            }  
	        }  
		return listTemp;
	}
	
	//退出
	public boolean exit(HttpServletRequest req) throws UserNotFoundException {
		HttpSession session = req.getSession();		
		session.invalidate();
		HttpSession newSession = req.getSession(false);
		if(newSession==null){
			return true;
		}
		return false;
	}


	//删除用户实际上是更新用户state状态
	public String updateState(HttpServletRequest request,String id) throws UserNotFoundException, IOException {
		if(id==null){
			throw new UserNotFoundException("请选择需要删除的用户!");
		}
		
		String org_id = request.getSession().getAttribute("org_id").toString();
		
		int up = userDao.updateState(id,org_id);
		if(up<=0){
			throw new UserNotFoundException("删除失败!");
		}else{
			//用户禁用成功后推送  @author linfa
	        //手机标识
//	        Integer deviceType = 3;
//	      	Integer deviceType = (Integer)request.getSession().getAttribute("deviceType");
			Integer deviceType = userChannelDao.findeviceType(id);
	        String channelId = userChannelDao.findChannelIdByUserId(id);
	        if(deviceType!=null&&channelId!=null){
				
		        //用于手机app前端数据展示
		        JSONObject jsonObject = new JSONObject();
		        jsonObject.put("tip","您的现场管理账号已被禁用！");
		        jsonObject.put("userId",id);
		        
		        //消息提示
		        String message = "";
		        String title = "你有一条账号禁用消息！";
		        Map<String,Object> map = new HashMap<String,Object>();
		        map.put("topic","disable");
		        map.put("data",jsonObject.toString());
		        map.put("message","");
		        map.put("state","");
		        JSONObject notification = PushInfo.PushFormat(deviceType, message, title, map);
		        
		        BaiduPush.pushNotificationByUser(channelId, deviceType, notification);
	        }
		}
		return "禁用成功！";
	}


	//批量删除用户实际上是批量更新用户state状态
	public String arrupdateState(HttpServletRequest request,String... ids) throws UserNotFoundException, PushServerException, PushClientException, IOException {
		if(ids.length==0){
			throw new UserNotFoundException("请选择需要删除的用户!");
		}
		
		String org_id = request.getSession().getAttribute("org_id").toString();
		
		List<String> list = Arrays.asList(ids);
		
		int up = userDao.arrUpdateState(list, org_id);
		
		int up1 = userDao.arrUpdateState1(list, org_id);
		//动态数组接收的推送channel_id
//		String[] channelIds = new String[ids.length];
		ArrayList<String> aList=new ArrayList<String>();
		
		if(ids.length!=up&&ids.length!=up1){
			throw new UserNotFoundException("删除失败!");
		}else{
			//用户禁用成功后推送  @author linfa
	        //手机标识
	        Integer deviceType = 3;
//	      	Integer deviceType = (Integer)request.getSession().getAttribute("deviceType");
			
			for(int i = 0;i<ids.length;i++){
				//获取channelids
				String channel_id = userChannelDao.findChannelIdByUserId(ids[i]);
				Integer type= userChannelDao.findeviceType(ids[i]);
				if(type!=null&&type==deviceType&&channel_id!=null){
					aList.add(channel_id);
				}
			}
	        //用于手机app前端数据展示
	        JSONObject jsonObject = new JSONObject();
	        jsonObject.put("tip","您的现场管理账号已被禁用！");
	        
	        //消息提示
	        String message = "";
	        String title = "你有一条账号禁用消息！";
	        Map<String,Object> map = new HashMap<String,Object>();
	        map.put("topic","disable");
	        map.put("data",jsonObject.toString());
	        map.put("message","");
	        map.put("state","");
	        
	        String[] channelIds = (String[])aList.toArray(new String[aList.size()]);  
	        JSONObject notification = PushInfo.PushFormat(deviceType, message, title, map);
	        BaiduPush.pushBatchUniMsg(channelIds, notification, deviceType);
			
		}
		return "禁用成功！";
	}
	
	//启用用户实际上是更新用户state状态
	public String enabledState(HttpServletRequest request,String id) throws UserNotFoundException {
		if(id==null){
			throw new UserNotFoundException("请选择需要启用的用户!");
		}
		
		String org_id = request.getSession().getAttribute("org_id").toString();
		
		int up = userDao.enabledState(id,org_id);
		if(up<=0){
			throw new UserNotFoundException("启用失败!");
		}
		return "启用成功！";
	}


	//根据部门查询该部门下人员
	public List<UserInfoAccount> throwDeptFindUser(HttpServletRequest request,String dept_no) throws UserNotFoundException {
		if(dept_no==null){
			throw new UserNotFoundException("请选择部门!");
		}
		String org_id = request.getSession().getAttribute("org_id").toString();
		
		//父查子部门id，含父id
		List<String> listDeptNo = deptDao.findDeptBylowPid(dept_no);
	//	System.out.println(listDeptNo);
		List<UserInfoAccount> listUserInfo = new ArrayList<UserInfoAccount>();
	//	System.out.println(listDeptNo.size());
		if(listDeptNo.size()==1){
			listUserInfo = userDao.throwDeptFindUser(dept_no,org_id);
		}
		if(listDeptNo.size()>1){
			//接收部门人员信息
			List<UserInfoAccount> list = new ArrayList<UserInfoAccount>();
			//根据dept_no查询top_dept_no,再查询部门pid是否为0
			List<String> listString= deptDao.throwUserChildAccountFindPid(dept_no,org_id);
		//	System.out.println(listString);
			//pid为0时，查询为顶级部门人员
			if(listString.size()==1&&"0".equals(listString.get(0))){
			//	System.out.println("1");
				listUserInfo = userDao.throwDeptFindUser(dept_no,org_id);
			}else{
				for(int i = 0; i<listDeptNo.size();i++){
					list.isEmpty();
					list = userDao.throwDeptFindUser(listDeptNo.get(i),org_id);
				//	System.out.println(list);
					listUserInfo.addAll(list);
				}
			}
			
		}
		return listUserInfo;
	}


	//查询101和102里面的全部领导
	public List<UserInfo> findAlleader(String org_id) throws UserNotFoundException {
		List<UserInfo> list = new ArrayList<UserInfo>();
		try {
			list = userDao.findAlleader(org_id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new UserNotFoundException("查询失败!");
		}
		
		return list;
	}


	//查询除了权限为0的所有用户
	public List<UserInfoAccount> CxUserXinXi(String org_id) throws UserNotFoundException {
		List<UserInfoAccount> list = new ArrayList<UserInfoAccount>();
		try {
			list = userDao.CxUserXinXi(org_id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new UserNotFoundException("查询失败!");
		}
		return list;
	}
	
	
	//查询所有用户信息,排除id为0的用户  包含名称的首字母，全拼音
	public List<Conver> conver(String org_id) throws UserNotFoundException {
		List<Conver> conver_list = new ArrayList<Conver>();
		try {
			conver_list = userDao.conver(org_id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new UserNotFoundException("查询失败!");
		}
		return conver_list;
	}

	//根据人物id查询人物信息
	public UserPart reasonIdfindUser(String id,String org_id) throws UserNotFoundException {
		UserPart userPart = new UserPart();
		try {
			userPart = userDao.reasonIdfindUser(id, org_id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new UserNotFoundException("加载失败!请稍后重试");
		}
		return userPart;
	}


	//查询所有用户信息
	public List<UserResult> allUserinfo(String org_id,String state) throws UserNotFoundException {
		List<UserResult> list = new ArrayList<UserResult>();
		try {
			list = userDao.allUserinfo(org_id,state);
		} catch (Exception e) {
			e.printStackTrace();
			throw new UserNotFoundException("加载失败!请稍后重试");
		}
		
		return list;
	}


	//更新密码 lisuoding
	public String updatepassword(HttpServletRequest request,String password, String oldpassword, String id) throws UserNotFoundException, IOException {
		if(password==null||password.trim().isEmpty()){
			throw new UserNotFoundException("密码不能为空");
		}
		String old = userDao.oldpassword(id);
		String olll = NoteUtil.md5(oldpassword);
		String org_id = request.getSession().getAttribute("org_id").toString();
		//密码复杂度
//		String passwordReg1 = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$";
		String passwordReg1 = "^((?=.*[a-z])(?=.*\\d)|(?=[a-z])(?=.*[#@!~%^&*])|(?=.*\\d)(?=.*[#@!~%^&*]))[a-z\\d#@!~%^&*]{6,20}$";
		if(olll.equals(old)){
			if(!Pattern.matches(passwordReg1,password)){
//				System.out.println(oldpassword+"=1"+olll+"=2"+old+"=3");
				throw new PasswordNotFoundException("登录密码需要填写6-20位字母数字组合！");
			}
		
			//密码加密
			String Md5UserPassword1 = NoteUtil.md5(password);
			if(id==null){
				throw new UserNotFoundException("id不能为空");
			}
			int i = userDao.updateUser2(Md5UserPassword1,id,org_id);
		
			if(i!=1){
				throw new UserNotFoundException("更新失败");
				
			}else{
				//更新成功后推送消息  @author linfa
		        //手机标识
//		        Integer deviceType = 3;
//		      	Integer deviceType = (Integer)request.getSession().getAttribute("deviceType");
				Integer deviceType = userChannelDao.findeviceType(id);
//		      	System.out.println("deviceType="+deviceType);
		        String channelId = userChannelDao.findChannelIdByUserId(id);
//		        System.out.println("channelId="+channelId);
		      	if(deviceType!=null&&channelId!=null){
					
			        //用于手机app前端数据展示
			        JSONObject jsonObject = new JSONObject();
			        jsonObject.put("tip","您的现场管理账号密码已被修改");
			        jsonObject.put("flag",Md5UserPassword1);
			        jsonObject.put("userId",id);
			        
			        //消息提示
			        String message = "";
			        String title = "你有一条新的密码修改消息！";
			        Map<String,Object> map = new HashMap<String,Object>();
			        map.put("topic","update_pwd");
			        map.put("data",jsonObject.toString());
			        map.put("message","");
			        map.put("state","");
			        JSONObject notification = PushInfo.PushFormat(deviceType, message, title, map);
			        
			        BaiduPush.pushNotificationByUser(channelId, deviceType, notification);
		      	}
			}
			
			
		}else{
			throw new UserNotFoundException("旧密码错误");
		}
		return "更新成功";

	}



	//退出1 lisuoding
	public boolean exit1(HttpServletRequest req) throws UserNotFoundException {
		HttpSession session = req.getSession();		
		session.invalidate();
		HttpSession newSession = req.getSession(false);
		if(newSession==null){
			return true;
		}
		return false;
	}
	
	//自动登录 liketao
	public boolean autoLogin(String account, String password, HttpServletRequest req)
			throws UserNotFoundException, PasswordNotFoundException {
		if(account==null||account.trim().isEmpty()){
			throw new UserNotFoundException("账号为空！");
		}
		if(password==null||password.trim().isEmpty()){
			throw new PasswordNotFoundException("密码为空！");
		}
//		int num = operateLogDao.countLogin(account);
//		//System.out.println("num="+num);
//		if(num>=5){
//			throw new UserNotFoundException("该账号已限制登录，请与管理员联系！");
//		}
		//password = NoteUtil.md5(password);
		
		List<Map<String,Object>> list = userDao.findUserAllInfoByAccount(account);
		//System.out.println("list"+list);
//			for(Map<String,Object> map:list){
//				System.out.println(map);
//			}
		if(list.isEmpty()){
			throw new UserNotFoundException("用户名不存在");
		}
		for(Map<String,Object> map:list){
			if(map.containsKey("state")){
				if(!"Y".equals(map.get("state"))){
					throw new UserNotFoundException("该用户不允许登录");
				}				
			}
			if(map.containsKey("password")){
				if(!password.equals(map.get("password"))){
					throw new PasswordNotFoundException("密码错误");
				}					
			};
		}	
		
		HttpSession session = req.getSession();
		//获取IP地址
		String ip = IPUtil.getLoginIp(req);
		
//			String ip = req.getHeader("x-forwarded-for");
//		    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
//		        ip = req.getHeader("Proxy-Client-IP"); 
//		    }  
//		    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
//		        ip = req.getHeader("WL-Proxy-Client-IP");
//		    }  
//		    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
//		        ip = req.getRemoteAddr();
//		       
//		    }
	    String id = null;
	    for(Map<String,Object> map:list){
	    	id = (String) map.get("id");
			session.setAttribute("id", id);//用户ID
			session.setAttribute("username", map.get("username"));//用户姓名
			session.setAttribute("nickname", map.get("nickname"));//用户昵称
			session.setAttribute("dept_no", map.get("dept_no"));//部门号
			session.setAttribute("top_dept_no", map.get("top_dept_no"));//组织号
			session.setAttribute("dept_flag", map.get("dept_flag"));//业主or监理or施工
			session.setAttribute("icon_url", map.get("face")==null?"":map.get("face"));	//头像
			session.setAttribute("org_id", map.get("org_id"));//组织标识
			//System.out.println("cs="+session.getId());
			//System.out.println("org_id="+map.get("org_id"));
				
		}
	    List<String> role_nos1 = roleUserDao.findRoleNoByUserId(id);
	    //List<String> role_nos2 = roleUserDao.findRoleNoByUserIdAndChild(id);
	    //role_nos1.addAll(role_nos2);
//	    System.out.println("role_nos1"+role_nos1);
	    session.setAttribute("role_no", role_nos1);//角色号
		session.setAttribute("account",account);//账号
		session.setAttribute("userip", ip);//用户登录IP
//		session.setAttribute("org_id", "1");//组织标识
		MemberParam param = new MemberParam();
		param.setUser_id(id);
		param.setOrg_id((String)session.getAttribute("org_id"));
		Member member = memberDao.findMember(param);
		String dept_id=member==null?"":member.getDept_id();
		session.setAttribute("dept_id", dept_id);//当组织ID
		return true;
	}


	//根据id查询用户信息  用于主页个人信息展示
	public UserIndexResult indexUserInfo(String id) throws UserNotFoundException {
		if(id==null){
			throw new UserNotFoundException("请登录！");
		}
		UserIndexResult indexUserInfo = userDao.indexUserInfo(id);
		return indexUserInfo;
	}


	//更新用户头像
	public String updateFace(String id,MultipartFile file) throws UserNotFoundException, IllegalStateException, IOException {
		
		if(id==null){
			throw new UserNotFoundException("请登录！");
		}
        String path=null;// 文件路径
        String type=null;// 文件类型
        if (file!=null) {// 判断上传的文件是否为空
 //       	System.out.println("size="+file.getSize());
            String fileName=file.getOriginalFilename();// 文件原名称
  //          System.out.println("上传的文件原名称:"+fileName);
            // 判断文件类型
            type=fileName.substring(fileName.indexOf("."));
  //          System.out.println(type);
            if (type!=null) {// 判断文件类型是否为空
	            // 自定义的文件名称
	            //String trueFileName = ;
	        	//文件夹
	        	String old_path = PathUtil.getPath()+"user";
	            // 设置存放图片文件的路径
	            path = old_path+"/"+attachService.findAttach_path_seq()+type;
	//            System.out.println("存放图片文件的路径:"+path);
	            // 转存文件到指定的路径
	            File filepath=new File(old_path);  
	            if(!filepath.exists()){//判断改路径是否存在  
	            	filepath.mkdirs();  
	            }
	            file.transferTo(new File(path));
	//            System.out.println("文件成功上传到指定目录下");
                }
            }else {
            	throw new UserNotFoundException("未上传任何图片！");
            }
        int update = userDao.updateFace(path,id);
        if(update<=0){
        	throw new UserNotFoundException("更新头像失败！");
        }
		return path;
	}


	//点击增加权限，查询用户列表，给用户增加权限
	public List<Object> UserLine(String org_id,String role_no,Integer page,Integer limit,String username) throws UserNotFoundException {
		
		List<Object> listMap = new ArrayList<Object>();
		//查询除了0权限以外的全部人员信息
		List<UserInfoAccount> list = new ArrayList<UserInfoAccount>();
		//分页
		Integer start = null;
		
		if(limit==null&&page!=null){
			limit = paging.getPageSize();
			start=(page-1)*limit;
		}
		if(page==null){
			start = null;
			limit = null;
		}
		if(limit!=null&&page!=null){
			start = limit*(page-1);
		}
		
		
		try {
			list = userDao.findAllUserLine(org_id, start, limit,username);
		} catch (Exception e) {
			e.printStackTrace();
			throw new UserNotFoundException("查询用户列表失败！");
		}
		
		String user_id = null;
		int count = 0;
		String account = null;
		String user_name = null;
		for(UserInfoAccount ua : list){
			Map<String,Object> mapArr = new LinkedHashMap<String,Object>();
			user_id = ua.getId();
			count = roleUserDao.findcountByRoleNoAndUserId(role_no, user_id);
			account = ua.getAccount();
			user_name = ua.getUsername();
			mapArr.put("user_id",user_id);
			mapArr.put("account",account);
			mapArr.put("user_name", user_name);
			if(count==0){
				mapArr.put("selected","");
			}
			if(count>0){
				mapArr.put("selected","selected");
			}
			listMap.add(mapArr);
		}
		return listMap;
	}


	//根据用户id重置用户密码
	public String restPassword(HttpServletRequest request,String user_id) throws UserNotFoundException {
		
		if(user_id == null){
			throw new UserNotFoundException("请选择用户id！");
		}
		
		String org_id = request.getSession().getAttribute("org_id").toString();
		String rest_password= null;
		try {
			rest_password = PropertiesUtil.readProperties("restPassword");
		} catch (IOException e) {
			e.printStackTrace();
		}
		int i  = userDao.restPassword(rest_password, user_id,org_id);
		if(i<=0){
			throw new UserNotFoundException("重置失败！请稍后重试");
		}
		return "重置成功！";
	}

	//用户列表统计查询
	public String findUserArrayCount(UserParam userParam, String org_id, String state) throws UserNotFoundException {
		userParam.setOrg_id(org_id);
		userParam.setState(state);
		String count = userDao.findUserArrayCount(userParam);
		return count;
	}

	//统计用户列表人数
	public String findUserLineCount(HttpServletRequest request,String username) throws UserNotFoundException {
		
		String org_id = request.getSession().getAttribute("org_id").toString();
		String count = null;
		try {
			count = userDao.findUserLineCount(org_id,username);
		} catch (Exception e) {
			e.printStackTrace();
			throw new UserNotFoundException("查询失败！请稍后重试");
		}
		return count;
	}


	//用于user查询多条件动态查询多表 将已经是党员的用户排除在外
	public List<UserResult> findUserByArrayNotInPartyUser(HttpServletRequest request,UserParam userParam, String state)
			throws UserNotFoundException {
		String org_id = request.getSession().getAttribute("org_id").toString();
		
//		String dept_no = request.getSession().getAttribute("dept_no").toString();
//		if(dept_no==null){
//			throw new UserNotFoundException("请登录！");
//		}
//		
//		//递归查询父往下查所有部门id
//		List<String> list_Dept = deptDao.findDeptBylowPid(dept_no);
//		System.out.println("2.......");
		
		Integer page = userParam.getPage();
		Integer limit = userParam.getLimit();
		Integer start = null;
		
		if(limit==null&&page!=null){
			limit = paging.getPageSize();
			start=(page-1)*limit;
		}
		if(page==null){
			start = null;
			limit = null;
		}
		if(limit!=null&&page!=null){
			start = limit*(page-1);
		}
		userParam.setOrg_id(org_id);
		userParam.setStart(start);
		userParam.setState(state);
		
		List<UserResult> listUserResult = new ArrayList<UserResult>();
		
		try {
			
			listUserResult = userDao.findUserByArrayNotInPartyUser(userParam);
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new UserNotFoundException("查询失败!请稍后重试");
		}	
		return listUserResult;
	}


	//用于user查询多条件动态查询多表 将已经是党员的用户排除在外 统计
	public String findUserByArrayNotInPartyUserCount(HttpServletRequest request,UserParam userParam,String state)
			throws UserNotFoundException {
		
		String org_id = request.getSession().getAttribute("org_id").toString();
		userParam.setOrg_id(org_id);
		userParam.setState(state);
		return userDao.findUserByArrayNotInPartyUserCount(userParam);
	}
	
	
	

}
